package org.fujay.langchain4jserver.domain.knowledge.entity;

import lombok.*;
import org.fujay.langchain4jserver.domain.knowledge.vo.DocumentCount;
import org.fujay.langchain4jserver.domain.knowledge.vo.KnowledgeBaseId;
import org.fujay.langchain4jserver.domain.knowledge.vo.KnowledgeBaseStatus;
import org.fujay.langchain4jserver.domain.model.vo.ModelId;

import java.time.LocalDateTime;

/**
 * 知识库实体，聚合根的一部分
 */
@Getter
@Setter(AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class KnowledgeBase {

    @NonNull
    private KnowledgeBaseId id;

    @NonNull
    private String name;

    private String description;

    @NonNull
    private String indexName;

    @NonNull
    private ModelId embeddingModelId;

    private String namespace;

    @NonNull
    private KnowledgeBaseStatus status;

    @NonNull
    private DocumentCount documentCount;

    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 工厂方法
    public static KnowledgeBase create(
            KnowledgeBaseId id,
            String name,
            String description,
            String indexName,
            ModelId embeddingModelId,
            String namespace) {
        return new KnowledgeBase(
                id,
                name,
                description,
                indexName,
                embeddingModelId,
                namespace,
                KnowledgeBaseStatus.EMPTY,
                DocumentCount.zero(),
                LocalDateTime.now(),
                LocalDateTime.now());
    }

    // 加载方法 (供仓储使用)
    public static KnowledgeBase load(
            KnowledgeBaseId id,
            String name,
            String description,
            String indexName,
            ModelId embeddingModelId,
            String namespace,
            KnowledgeBaseStatus status,
            DocumentCount documentCount,
            LocalDateTime createTime,
            LocalDateTime updateTime) {
        return new KnowledgeBase(
                id,
                name,
                description,
                indexName,
                embeddingModelId,
                namespace,
                status,
                documentCount,
                createTime,
                updateTime);
    }

    // 更新基础信息
    public void updateInfo(String name, String description, ModelId embeddingModelId) {
        this.name = name;
        this.description = description;
        this.embeddingModelId = embeddingModelId;
        this.updateTime = LocalDateTime.now();
    }

    // 内部方法，供聚合根调用，用于更新状态和文档数
    public void setStatus(KnowledgeBaseStatus newStatus) {
        if (this.status != newStatus) {
            this.status = newStatus;
            this.updateTime = LocalDateTime.now();
        }
    }

    public void setDocumentCount(DocumentCount newCount) {
        if (!this.documentCount.equals(newCount)) {
            this.documentCount = newCount;
            this.updateTime = LocalDateTime.now();
        }
    }
}
